/******************************************
Cola de chars
Archivo: queue_fun.c, 11/4/2011, sebfer
Modificado para el LAB5, 13/4/2011
*****************************************/

#include "queue.h"

static int head;
static int tail;
static char q_elements[QUEUE_SIZE];

static int head_tx;
static int tail_tx;
static char q_tx_elements[QUEUE_SIZE];

void q_init(){
  // reset position index
  head = 0; 
  tail = 0;
}

void q_add(char in){
//  assert(!q_isfull()); // if queue if full => abort
  q_elements[head] = in;
  head++;
  if (head == QUEUE_SIZE){
    head = 0;
  }
}

char q_get(){
  char data_to_return;
//  assert(!q_isempty()); // if queue if empty => abort
  data_to_return = q_elements[tail];
  tail++;
  if (tail == QUEUE_SIZE){
     tail = 0;
  }
  return data_to_return;
}

int q_isempty(){
  if  (tail == head ){
    return 1; // true, queue is empty
  } else {
    return 0; // false, queue is not empty
  }
}

int q_isfull(){
  if ( (head+1 == tail) || ((head+1 == QUEUE_SIZE) && (tail == 0) ) ){
    return 1; // true, queue is full
  } else {
    return 0; // false, queue is not full
  }
}

void q_tx_init(){
  // reset position index
  head_tx = 0; 
  tail_tx = 0;
}

void q_tx_add(char in){
//  assert(!q_tx_isfull()); // if queue if full => abort
  q_tx_elements[head_tx] = in;
  head_tx++;
  if (head_tx == QUEUE_SIZE){
    head_tx = 0;
  }
}

char q_tx_get(){
  char data_to_return;
//  assert(!q_tx_isempty()); // if queue if empty => abort
  data_to_return = q_tx_elements[tail_tx];
  tail_tx++;
  if (tail_tx == QUEUE_SIZE){
     tail_tx = 0;
  }
  return data_to_return;
}

int q_tx_isempty(){
  if  (tail_tx == head_tx ){
    return 1; // true, queue is empty
  } else {
    return 0; // false, queue is not empty
  }
}

int q_tx_isfull(){
  if ( (head_tx+1 == tail_tx) || ((head_tx+1 == QUEUE_SIZE) && (tail_tx == 0) ) ){
    return 1; // true, queue is full
  } else {
    return 0; // false, queue is not full
  }
}
